package ahblue.cn.tanzi.ui.circle;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shehuan.nicedialog.BaseNiceDialog;
import com.shehuan.nicedialog.NiceDialog;
import com.shehuan.nicedialog.ViewConvertListener;
import com.shehuan.nicedialog.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import uk.co.senab.photoview.PhotoView;

public class PlusImageActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    public static final String ARG_IMG_LIST = "ARG_IMG_LIST"; // 图片
    public static final String ARG_POSITION = "ARG_POSITION"; // 第几张图片
    public static final int RESULT_CODE_VIEW_IMG = 11; //查看大图页面的结果码

    @BindView(R.id.viewPager)
    ViewPager viewPager; // 展示图片的ViewPager
    @BindView(R.id.position_tv)
    TextView positionTv; // 图片的位置，第几张图片

    private ArrayList<String> imgList; // 图片的数据源
    private int mPosition; // 第几张图片
    private ViewPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plus_image);

        ButterKnife.bind(this);

        imgList = getIntent().getStringArrayListExtra(ARG_IMG_LIST);
        mPosition = getIntent().getIntExtra(ARG_POSITION, 0);
        initView();
    }

    private void initView() {
        viewPager.addOnPageChangeListener(this);

        mAdapter = new ViewPagerAdapter(this, imgList);
        viewPager.setAdapter(mAdapter);
        positionTv.setText(mPosition + 1 + "/" + imgList.size());
        viewPager.setCurrentItem(mPosition);
    }

    @OnClick(R.id.back_iv)
    void onClickBack() {
        back();
    }

    @OnClick(R.id.delete_iv)
    void onClickDelete() {
        deletePic();
    }

    //删除图片
    private void deletePic() {
        NiceDialog.init()
                .setLayoutId(R.layout.dialog_cancel_confrim)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    protected void convertView(ViewHolder viewHolder, BaseNiceDialog baseNiceDialog) {
                        TextView textViewMessage = viewHolder.getView(R.id.textViewMessage);
                        textViewMessage.setText("确定要删除这张照片吗？");

                        viewHolder.setOnClickListener(R.id.textViewConfirm, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                imgList.remove(mPosition); //从数据源移除删除的图片
                                setPosition();
                                baseNiceDialog.dismiss();
                            }
                        });

                        viewHolder.setOnClickListener(R.id.textViewCancel, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                baseNiceDialog.dismiss();
                            }
                        });
                    }
                })
                .setMargin(50)
                .show(getSupportFragmentManager());
    }

    //设置当前位置
    private void setPosition() {
        positionTv.setText(mPosition + 1 + "/" + imgList.size());
        viewPager.setCurrentItem(mPosition);
        mAdapter.notifyDataSetChanged();
    }

    //返回上一个页面
    private void back() {
        Intent intent = getIntent();
        intent.putStringArrayListExtra(ARG_IMG_LIST, imgList);
        setResult(RESULT_CODE_VIEW_IMG, intent);

        finish();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mPosition = position;
        positionTv.setText(position + 1 + "/" + imgList.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //按下了返回键
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }





    class ViewPagerAdapter extends PagerAdapter {

        private Context context;
        private List<String> imgList; //图片的数据源

        ViewPagerAdapter(Context context, List<String> imgList) {
            this.context = context;
            this.imgList = imgList;
        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            return POSITION_NONE;
        }

        @Override
        public int getCount() {
            return imgList.size();
        }

        //判断当前的View 和 我们想要的Object(值为View) 是否一样;返回 true/false
        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return (view == object);
        }

        //instantiateItem()：将当前view添加到ViewGroup中，并返回当前View
        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View itemView = getItemView(R.layout.view_pager_img);
            PhotoView imageView = (PhotoView) itemView.findViewById(R.id.img_iv);
            Glide.with(context).load(imgList.get(position)).into(imageView);
            container.addView(itemView);
            return itemView;
        }

        //destroyItem()：删除当前的View;
        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

        private View getItemView(int layoutId) {
            return LayoutInflater.from(this.context).inflate(layoutId, null, false);
        }
    }
}
